<?php
class ControllerSetting extends Controller {
	var $error = array();

	function index() { 
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');
		$session  =& $this->locator->get('session');
		$cache    =& $this->locator->get('cache');

		$language->load('controller/setting.php');

		$template->set('title', $language->get('heading_title'));

		$setting = array();
		$setting[] = 'global_config_email';
		$setting[] = 'catalog_config_checkout_id';
		$setting[] = 'catalog_config_account_id';
		$setting[] = 'catalog_config_vat';
		$setting[] = 'catalog_config_stock_subtract';
		$setting[] = 'catalog_config_stock_checkout';
		$setting[] = 'catalog_config_stock_check';
		$setting[] = 'global_config_order_status_id';
		$setting[] = 'global_config_tax';
		$setting[] = 'global_config_weight_class_id';
		$setting[] = 'global_config_currency';
		$setting[] = 'admin_config_language';
		$setting[] = 'catalog_config_language';
		$setting[] = 'global_config_zone_id';
		$setting[] = 'global_config_country_id';
		$setting[] = 'admin_config_ssl';
		$setting[] = 'admin_config_parse_time';
		$setting[] = 'admin_config_max_rows';
		$setting[] = 'admin_config_template';
		$setting[] = 'catalog_config_ssl';
		$setting[] = 'catalog_config_parse_time';
		$setting[] = 'catalog_config_url_alias';
		$setting[] = 'catalog_config_max_rows';
		$setting[] = 'catalog_config_template';
		$setting[] = 'global_config_fax';
		$setting[] = 'global_config_telephone';
		$setting[] = 'global_config_address';
		$setting[] = 'global_config_owner';
		$setting[] = 'global_config_store';
		$setting[] = 'global_config_email_send';
		$setting[] = 'catalog_config_email_send_order';
		$setting[] = 'catalog_config_email_send_account_create';
		$setting[] = 'global_config_cache_query';
		$setting[] = 'global_config_compress_output';
		$setting[] = 'global_config_compress_level';
		$setting[] = 'global_config_image_resize';
		$setting[] = 'catalog_config_download';
		$setting[] = 'catalog_config_download_status';
		$setting[] = 'global_config_maintenance';
		$setting[] = 'global_config_image_width';
		$setting[] = 'global_config_image_height';
		$setting[] = 'catalog_config_image_width_product';
		$setting[] = 'catalog_config_image_height_product';
		$setting[] = 'catalog_config_image_width_category';
		$setting[] = 'catalog_config_image_height_category';
		$setting[] = 'catalog_config_image_width_images';
		$setting[] = 'catalog_config_image_height_images';
		$setting[] = 'catalog_config_image_width_related';
		$setting[] = 'catalog_config_image_height_related';
		$setting[] = 'catalog_config_ajaxadd';
		$setting[] = 'global_config_session_expire';
		$setting[] = 'global_config_email_authentication';
		$setting[] = 'global_config_meta_keys_store';
		$setting[] = 'global_config_meta_description_store';

		if ($request->isPost() && $request->has('global_config_store', 'post') && $this->validate()) {
			$database->query("delete from setting where `group` = 'config'");

			foreach ($setting as $name) {
				list($type,$key)=explode('_',$name,2);
				$database->query($database->parse("insert into setting set type = '?', `group` = 'config', `key` = '?', `value` = '?'", $type, $key, $request->gethtml($name, 'post')));
			}
 
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('setting'));
		}

		$view = $this->locator->create('template');

		$view->set('tpl', $template);

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('text_yes', $language->get('text_yes'));
		$view->set('text_no', $language->get('text_no'));
		$view->set('text_none', $language->get('text_none'));

		$view->set('entry_store', $language->get('entry_store'));
		$view->set('entry_owner', $language->get('entry_owner'));
		$view->set('entry_address', $language->get('entry_address'));
		$view->set('entry_telephone', $language->get('entry_telephone'));
		$view->set('entry_fax', $language->get('entry_fax'));
		$view->set('entry_template', $language->get('entry_template'));
		$view->set('entry_url_alias', $language->get('entry_url_alias'));
		$view->set('entry_parse_time', $language->get('entry_parse_time'));
		$view->set('entry_ssl', $language->get('entry_ssl'));
		$view->set('entry_rows_per_page', $language->get('entry_rows_per_page'));
		$view->set('entry_country', $language->get('entry_country'));
		$view->set('entry_zone', $language->get('entry_zone'));
		$view->set('entry_language', $language->get('entry_language'));
		$view->set('entry_currency', $language->get('entry_currency'));
		$view->set('entry_weight', $language->get('entry_weight'));
		$view->set('entry_tax', $language->get('entry_tax'));
		$view->set('entry_order_status', $language->get('entry_order_status'));
		$view->set('entry_stock_check', $language->get('entry_stock_check'));
		$view->set('entry_stock_checkout', $language->get('entry_stock_checkout'));
		$view->set('entry_stock_subtract', $language->get('entry_stock_subtract'));
		$view->set('entry_vat', $language->get('entry_vat'));
		$view->set('entry_account', $language->get('entry_account'));
		$view->set('entry_checkout', $language->get('entry_checkout'));
		$view->set('entry_email', $language->get('entry_email'));
		$view->set('entry_email_send', $language->get('entry_email_send'));
		$view->set('entry_email_send_order', $language->get('entry_email_send_order'));
		$view->set('entry_email_send_account_create', $language->get('entry_email_send_account_create'));
		$view->set('entry_cache_query', $language->get('entry_cache_query'));
		$view->set('entry_compress_output', $language->get('entry_compress_output'));
		$view->set('entry_compress_level', $language->get('entry_compress_level'));
		$view->set('entry_image_resize', $language->get('entry_image_resize'));
		$view->set('entry_image_width', $language->get('entry_image_width'));
		$view->set('entry_image_height', $language->get('entry_image_height'));
		$view->set('entry_download', $language->get('entry_download'));
		$view->set('entry_download_status', $language->get('entry_download_status'));
		$view->set('entry_maintenance', $language->get('entry_maintenance'));
	 	$view->set('entry_ajaxadd', $language->get('entry_ajaxadd'));
	 	$view->set('entry_image_misc', $language->get('entry_image_misc'));
	 	$view->set('entry_image_product', $language->get('entry_image_product'));
	 	$view->set('entry_image_category', $language->get('entry_image_category'));
	 	$view->set('entry_image_images', $language->get('entry_image_images'));
	 	$view->set('entry_image_related', $language->get('entry_image_related'));
	 	$view->set('entry_config_session_expire', $language->get('entry_config_session_expire'));
	 	$view->set('entry_email_authentication', $language->get('entry_email_authentication'));
	 	$view->set('entry_meta_keys', $language->get('entry_meta_keys'));
		$view->set('entry_meta_description', $language->get('entry_meta_description'));
	 	
		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('tab_shop', $language->get('tab_shop'));
		$view->set('tab_admin', $language->get('tab_admin'));
		$view->set('tab_local', $language->get('tab_local'));
		$view->set('tab_stock', $language->get('tab_stock'));
		$view->set('tab_option', $language->get('tab_option'));
		$view->set('tab_mail', $language->get('tab_mail'));
		$view->set('tab_cache', $language->get('tab_cache'));
		$view->set('tab_image', $language->get('tab_image'));
		$view->set('tab_download', $language->get('tab_download'));
		$view->set('tab_meta', $language->get('tab_meta'));

		$view->set('error', @$this->error['message']);
		$view->set('error_store', @$this->error['store']);
		$view->set('error_owner', @$this->error['owner']);
		$view->set('error_email', @$this->error['email']);
		$view->set('error_address', @$this->error['address']);
		$view->set('error_telephone', @$this->error['telephone']);
		
		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('action', $url->ssl('setting'));
		
		$view->set('cancel', $url->ssl('setting'));

		$results = $database->getRows("select * from setting where `group` = 'config'");

		$setting_info = array();

		foreach ($results as $result) {
			$name = $result['type'].'_'.$result['key'];
			$setting_info[$name] = $result['value'];
		}

		foreach ($setting as $name) {
			$value=isset($setting_info[$name])?$setting_info[$name]:'';
			$view->set($name, $request->has($name, 'post')?$request->gethtml($name, 'post'):$value);
		}

		$template_data = array();

		foreach (glob(DIR_CATALOG_TEMPLATE . '*', GLOB_ONLYDIR) as $directory) {
			if (basename($directory) != DIR_TPL_CORE) {
				$template_data[] = basename($directory);
			}
		}

		$view->set('catalog_templates', $template_data);

		$template_data = array();

		foreach (glob(DIR_TEMPLATE . '*', GLOB_ONLYDIR) as $directory) {
			if (basename($directory) != DIR_TPL_CORE) {
				$template_data[] = basename($directory);
			}
		}

		$view->set('admin_templates', $template_data);
						
		$view->set('informations', $database->cache('information-' . (int)$language->getId(), "select * from information i left join information_description id on (i.information_id = id.information_id) where i.status = '1' and id.language_id = '" . (int)$language->getId() . "' order by i.sort_order"));

		$view->set('countries', $database->cache('country', "select country_id, name from country where status = '1' order by name"));

		$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));

		$view->set('languages', $database->cache('language', "select * from language order by sort_order"));

		$view->set('currencies', $database->cache('currency', "select * from currency"));

		$view->set('weight_classes', $database->cache('weight_class-' . (int)$language->getId(), "select weight_class_id, title from weight_class where language_id = '" . (int)$language->getId() . "'"));

		$view->set('order_statuses', $database->cache('order_status-' . (int)$language->getId(), "select order_status_id, name from order_status where language_id = '" . (int)$language->getId() . "' order by name"));

		$template->set('content', $view->fetch('content/setting.tpl'));

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function validate() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$user     =& $this->locator->get('user');
        $validate =& $this->locator->get('validate');
			
		if (!$user->hasPermission('modify', 'setting')) {
			$this->error['message'] = $language->get('error_permission');
		}
		
        if (!$validate->strlen($request->gethtml('global_config_store', 'post'),1,255)) {
			$this->error['store'] = $language->get('error_store');
		}

        if (!$validate->strlen($request->gethtml('global_config_owner', 'post'),1,255)) {
			$this->error['owner'] = $language->get('error_owner');
		}
		
		if (!$validate->strlen($request->gethtml('global_config_email', 'post'),1,255)) {
			$this->error['email'] = $language->get('error_email');
		}

		if (!$this->error) { return TRUE; }
	}

	function zone() {
		$html_select = '<select name="%s">%s</select>';
		$html_option = '<option value="%s"%s>%s</option>';

		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');

		$results = $database->cache('zone-' . (int)$request->gethtml('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->gethtml('country_id') . "' order by name");

		
		$output = sprintf($html_option,0,'',$language->get('text_none'));
		
		if ($results) {
			$output = '';
			foreach ($results as $result) {
				$selected = ($request->gethtml('zone_id') == $result['zone_id'])?' SELECTED':'';
				$output .= sprintf($html_option,$result['zone_id'],$selected,$result['name']);
			}
		}

		$response->set(sprintf($html_select,'global_config_zone_id',$output));
	}
}
?>
